<template>
  <div class="requirementDetail-container">
    <div
      v-if="detailsData"
      class="app-title-container title-position"
    >
      <modules-title title-name="检验依据审批详情">
        <template slot="btn">
          <el-button
            plain
            @click="goBack"
          >返回</el-button>
          <!-- <el-button
            v-hasPermi="{v:['quality:submit'],isF: detailsData.quality_standard_status==2 && isAudit}"
            type="primary"
            @click="auditOperation"
          >{{ auditButtonName }}</el-button> -->
          <approval
            :affair.sync="auditData"
            :is-text="false"
            @refresh="refreshData"
            @submit="onSubmit"
            @toEdit="toEdit"
            @cancel="onCancel"
          />
        </template>
      </modules-title>
    </div>
    <div class="main-content-container">

      <detail-content :details-data="detailsData" />

      <div class="tab-content">
        <comment-view
          :datas="auditData"
          is-show
        />
      </div>
    </div>
    <!-- 审批 -->
    <!-- <audit-dialog :id="qsid" ref="auditDialog" :audit-data="auditData" @auditSuccess="auditSuccess" /> -->
  </div>
</template>

<script>
import {
  qualitySourceDetail,
  qualitySourceDetailByFlowId,
  changeStatus,
  submitAudit,
  approval
} from '@/api/qualitySource'
export default {
  name: 'QualityDetail',
  components: {
    DetailContent: () => import('./modules/detailContent.vue'),
    AuditDialog: () => import('../modules/auditDialog.vue')
  },
  data() {
    return {
      userInfo: null,
      qsid: '', // 检验依据id
      flowId: '',
      detailsData: null,
      auditData: {},
      auditButtonName: '审批',
      isAudit: false,
      states: 'Done'
    }
  },
  // 生命周期 - 创建完成（访问当前this实例）
  created() {
    this.userInfo = this.$store.getters.userInfo
    this.flowId = this.$route.query.f_id || ''
    this.states = this.$route.query.state || 'Done'
    if (this.flowId) {
      if (this.states === 'Done') {
        this.getQualitySourceDetailByFlowId(this.flowId)
      }
      this.getAuditData(this.flowId)
    }
  },
  methods: {
    goBack() {
      this.$router.back()
    },
    editQuality() {
      this.$router.push({
        name: 'createQuality',
        query: {
          qualitySourceId: this.qsid
        }
      })
    },
    getAuditData(id) {
      this.$api.PROJECT_START_CHECK_DETAIL(id).then((res) => {
        if (res.code === 200) {
          this.auditData = res.data
          this.getButton()
        }
      })
    },
    getButton() {
      if (
        this.auditData.State !== 'Done' ||
        this.auditData.State !== 'Refuse'
      ) {
        if (this.auditData.Next && this.auditData.Next.length) {
          for (let i = 0; i < this.auditData.Next.length; i++) {
            const data = this.auditData.Next[i]
            const index = data.Staffs.findIndex(
              (a) => a === this.userInfo.user.id
            )
            if (index !== -1) {
              this.isAudit = true
              this.auditButtonName = data.ItemName
              break
            } else {
              this.isAudit = false
            }
          }
        }
      }
      if (this.auditData.State !== 'Done') {
        this.detailsData = this.auditData.Template.FieldValues.data
        this.qsid = this.auditData.Template.FieldValues.data.id
      }
    },
    // flowId获取检验依据详情
    getQualitySourceDetailByFlowId(id) {
      const datas = { id: id }
      qualitySourceDetailByFlowId(datas).then((res) => {
        if (res.code === 200) {
          this.detailsData = res.data
          this.qsid = res.data.id
        }
      })
    },
    // 获取检验依据详情
    getQualitySourceDetail(id) {
      const datas = { id: id }
      qualitySourceDetail(datas).then((res) => {
        if (res.code === 200) {
          this.detailsData = res.data
        }
      })
    },
    submitApproval() {
      submitAudit({ id: this.qsid }).then((res) => {
        if (res.code === 200) {
          this.getQualitySourceDetail(this.qsid)
        } else {
          this.$message({
            type: 'error',
            message: res.msg,
            duration: 1500
          })
        }
      })
    },
    stopItem() {
      this.changeStatusOperation(this.qsid, 5)
    },
    startItem() {
      this.changeStatusOperation(this.qsid, 4)
    },
    changeStatusOperation(id, status) {
      const datas = { id: id, quality_standard_status: status }
      changeStatus(datas).then((res) => {
        if (res.code === 200) {
          this.getQualitySourceDetail(this.qsid)
        } else {
          this.$message({
            type: 'error',
            message: res.msg,
            duration: 1500
          })
        }
      })
    },
    approvalOperation(datas, callback) {
      approval(datas).then((res) => {
        if (res.code === 200) {
          this.$message({
            type: 'success',
            message: '审批成功'
          })
          this.getQualitySourceDetailByFlowId(this.flowId)
          this.getAuditData(this.flowId)
          callback(true)
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          })
        }
      })
    },
    refreshData() {
      this.getQualitySourceDetailByFlowId(this.flowId)
      this.getAuditData(this.flowId)
    },
    toEdit: function(affairs) {
      console.log(affairs)
      this.$router.push({
        name: 'createQuality',
        query: {
          qualitySourceId: this.qsid
        }
      })
    },
    // affairsId, stepId , type  //选择类型, template //备注, RejectStepId  //步骤id, isVisible
    onSubmit: function(
      affairsId,
      stepId,
      type,
      template,
      RejectStepId,
      callback
    ) {
      console.log(affairsId, stepId, type, template, RejectStepId)
      const subdata = {
        id: this.qsid,
        quality_standard_status: type,
        idea: template.Remark,
        Files: template.Files
      }
      console.log(subdata)
      this.approvalOperation(subdata, callback)
    },
    onCancel(affairsId, stepId, type, template, RejectStepId, callback) {
      this.$alert('是否确认撤销？', '撤销', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(() => {
          const subdata = {
            flow_id: this.flowId,
            model_type: 'QualitySource'
          }
          this.$comFun
            .cancelFunc(subdata)
            .then(() => {
              callback(true)
              this.$message({
                type: 'success',
                message: '已撤销'
              })
              this.getQualitySourceDetailByFlowId(this.flowId)
              this.getAuditData(this.flowId)
            })
            .catch((err) => {
              this.$message.error(err)
            })
        })
        .catch((err) => {
          console.log(err)
        })
    }
    // auditOperation() {
    //   this.$refs.auditDialog.open()
    // },
    // auditSuccess() {
    //   this.$route.params.refresh = true // 返回时刷新列表
    //   this.getQualitySourceDetail(this.qsid)
    //   this.getAuditData(this.flowId)
    // },
  }
}
</script>
<style lang="scss" scoped>
/* @import url(); 引入css类 */
.requirementDetail-container {
  padding-top: 110px;
  box-sizing: border-box;
}
</style>
